package com.tongji.housediy.view
{
	import com.tongji.housediy.events.GetHouseByIDEvent;
	import com.tongji.housediy.events.TypeXMLAdminEvent;
	import com.tongji.housediy.events.brandEvent.GetAllBrandEvent;
	import com.tongji.housediy.events.brandEvent.GetGoodsByBrandEvent;
	import com.tongji.housediy.events.commonEvent.ResetRadioGroupEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideControlPanelEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.events.goodsEvent.DataDoneEvent;
	import com.tongji.housediy.events.goodsEvent.GetBrandNameEvent;
	import com.tongji.housediy.events.goodsEvent.GetBrandPicSrcEvent;
	import com.tongji.housediy.events.goodsEvent.GetCollectionEvent;
	import com.tongji.housediy.events.goodsEvent.GetGoodsByDictEvent;
	import com.tongji.housediy.events.goodsEvent.SearchGoodsEvent;
	import com.tongji.housediy.model.BrandModel;
	import com.tongji.housediy.model.CompEnableStateModel;
	import com.tongji.housediy.model.DesignModel;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.HouseModel;
	import com.tongji.housediy.model.RightCompStateModel;
	import com.tongji.housediy.model.RightListStateModel;
	import com.tongji.housediy.model.SearchResultModel;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.utils.PopUpAdmin;
	import com.tongji.housediy.utils.TextFieldPool;
	import com.tongji.housediy.view.RightControlComp;
	
	import de.polygonal.ds.Collection;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.utils.Endian;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	import spark.events.TextOperationEvent;
	
	/**
	 * 
	 * @author kevin
	 * 
	 */
	public class FirstCompMediator extends Mediator
	{
		[Inject]
		public var view:FirstComp;
		
		[Inject]
		public var model:RightCompStateModel;
	
		[Inject]
		public var stateModel:RightListStateModel;
		
		[Inject]
		public var designModel:DesignModel;
		
		[Inject]
		public var enableModel:CompEnableStateModel;
		
		[Inject]
		public var brandModel:BrandModel;
		
		[Inject]
		public var userModel:UserInforModel;
		
		private var currentState:String = "State1";
		
		[Inject]
		public var  brandModel2:SearchResultModel;
		[Inject]
		public var houseModel:HouseModel;
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		override public function onRegister():void
		{
			// 加载 下拉列表框 XML 数据
			dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.LOAD_TYPE_XML));
				
			BindingUtils.bindProperty(view.firstLayorShowComp, "currentState", model, "state");	
			BindingUtils.bindProperty(view.buildingTabBtn, "enabled", enableModel, "isRightTabBtnEnable");
			BindingUtils.bindProperty(view.furnitureTabBtn, "enabled", enableModel, "isRightTabBtnEnable");
			BindingUtils.bindProperty(view.myDIYTabBtn, "enabled", enableModel, "isRightTabBtnEnable");

			view.furnitureTabBtn.addEventListener(MouseEvent.CLICK, furnitureTabClickHandler);
			view.buildingTabBtn.addEventListener(MouseEvent.CLICK, buildingTabClickHandler);
			
			//切换到我的DIY状态下
			view.myDIYTabBtn.addEventListener(MouseEvent.CLICK, myDIYTabClickHandler);
			view.allBrandBtn.addEventListener(MouseEvent.CLICK, showAllBrandHandler);
			
			//切换到我的收藏状态下 xiangye
			view.collectBtn.addEventListener(MouseEvent.CLICK, showMyCollectHandler);
			
			// 加载 doorGridList，windowGridList，floorGridList 数据
			dispatch(new GetGoodsByDictEvent(GetGoodsByDictEvent.GET_GOODS_BY_DICT, "door"));
			dispatch(new GetGoodsByDictEvent(GetGoodsByDictEvent.GET_GOODS_BY_DICT, "window"));
			dispatch(new GetGoodsByDictEvent(GetGoodsByDictEvent.GET_GOODS_BY_DICT, "floor"));
			dispatch(new GetGoodsByDictEvent(GetGoodsByDictEvent.GET_GOODS_BY_DICT, "furniture"));
			dispatch(new GetGoodsByDictEvent(GetGoodsByDictEvent.GET_GOODS_BY_DICT, "wall"));
			
			dispatch(new GetAllBrandEvent(GetAllBrandEvent.GET_BRANDS_XML));
			//this.addContextListener(GetAllBrandEvent.GET_ALL_BRANDS, showAllBrandHandler, GetAllBrandEvent);
			dispatch(new GetAllBrandEvent(GetAllBrandEvent.GET_ALL_BRANDS));
			// 初始化 LabelPool
			TextFieldPool.initialize(5, 5);
			
			this.addContextListener(ResetRadioGroupEvent.RESET_TAB_RADIO, resetTabRadioHandler, ResetRadioGroupEvent);
            /*addContextListener(GetHouseByIDEvent.UPDATE_HOUSE_DETAIL, createHouseHandler, GetHouseByIDEvent);*/
	        addContextListener(GetGoodsByDictEvent.UPDATE_GOODS_MODEL, createHouseHandler, GetGoodsByDictEvent);
		}
		private function createHouseHandler(event:GetGoodsByDictEvent):void
		{
			if (goodsModel.doorArrayCollection != null && goodsModel.windowArrayCollection != null && goodsModel.floorArrayCollection != null 
				&& goodsModel.furnitureArrayCollection != null && goodsModel.wallArrayCollection != null && houseModel.house != null)
				roomModel.openHouse(new XML(houseModel.house.houseModelSrc));
		}
		private function furnitureTabClickHandler(event:MouseEvent):void
		{			
			view.firstLayorShowComp.currentState = 'State2';
			currentState = "State2";
			view.currentState = "normal";
			stateModel.stateIndex = RightListStateModel.FURNITURE_INDEX;
			dispatch(new ResetRadioGroupEvent(ResetRadioGroupEvent.RESET_FURNITURE_RADIO));
			
			// 重置 Level1 Furniture List
			dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.UPDATE_FURNITURE_LEVEL1_XML, "room"));
		}
		
		private function buildingTabClickHandler(event:MouseEvent):void
		{

			view.firstLayorShowComp.currentState = 'State1';
			currentState = "State1";
			view.currentState = "normal";
			view.tabGroup.selection = view.buildingTabBtn;
			stateModel.stateIndex = RightListStateModel.BUILDING_INDEX;
			dispatch(new ResetRadioGroupEvent(ResetRadioGroupEvent.RESET_BUILDING_RADIO));
		}
		
		private function myDIYTabClickHandler(event:MouseEvent):void
		{
			if (!userModel.isLogined)
			{
				PopUpAdmin.popupCenterWindow("notLogin", view.parent, mediatorMap);
			}
			else
			{
				view.firstLayorShowComp.currentState = 'State8'; 
				currentState = "State8";
				view.currentState = "normal";
				view.buildingTabBtn.enabled = true;
				BindingUtils.bindProperty(view.firstLayorShowComp.designList.designList.dataGroup, "dataProvider", designModel, "designArray");
				view.tabGroup.selection = view.myDIYTabBtn;
			}			
		}
			
		private function resetTabRadioHandler(event:ResetRadioGroupEvent):void
		{
			view.tabGroup.selection = view.buildingTabBtn;
		}
				
		public function showAllBrandHandler(event:MouseEvent):void
		{								
			view.allBrandBtn.selected = true;
			
			if (enableModel.isSearchState)
			{
				view.currentState = "searchAndNews";				
			}
			else
			{
				view.currentState = "normalAndNews";				
			}
			view.secondLayorShowComp.currentState = "State2";
			//view.secondLayorShowComp.brandsViewStack.selectedIndex = 0;
		}
		
		public function showMyCollectHandler(event:MouseEvent):void
		{
			if(userModel.userID != "")
			{
				dispatch(new GetCollectionEvent(GetCollectionEvent.GET_COLLECTIONS, userModel.userID, null));
				view.collectBtn.selected = true;
				if (enableModel.isSearchState)
				{
					view.currentState = "searchAndNews";				
				}
				else
				{
					view.currentState = "normalAndNews";				
				}
				view.secondLayorShowComp.currentState = "State1";
			}
			else
			{
				PopUpAdmin.popupCenterWindow("notLogin", view.parent, mediatorMap);
			}
					
		}
		
	}
}